package com.test.simplechat.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制层
 */
@Controller
@CrossOrigin
public class UrlController {
    private final static Logger LOGGER = LoggerFactory.getLogger(UrlController.class);
    @Value("${ws.port}")
    private String wsPort;
    @Value("${ws.path}")
    private String wsPath;

    /**
     * 统一跳转
     *
     * @param dir
     * @param template
     * @return
     */
    @GetMapping({"/{template}.shtml", "/{dir}/{template}.shtml"})
    public String url(ModelMap modelMap, @PathVariable(value = "dir", required = false) final String dir, @PathVariable("template") final String template, HttpServletRequest request) {
        String username = request.getParameter("username");

        StringBuffer sbf = new StringBuffer(template);
        if (dir != null && !"".equals(dir)) {
            sbf.insert(0,  dir + "/");
        }
        modelMap.addAttribute("wsPort", wsPort);
        modelMap.addAttribute("wsPath", wsPath);
        if (username != null && !"".equals(username)) {
            modelMap.addAttribute("username", username);
        }
        return sbf.toString();
    }
}
